package com.dbpms.controller;

import com.dbpms.pojo.Noticeinfo;
import com.dbpms.pojo.User;
import com.dbpms.service.INoticeService;
import com.dbpms.until.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService iNoticeService;

    /**
     * 查询公告信息
     * @param param
     * @param model
     * @return
     */
    @GetMapping("/listInfo")
    public String noticeInfos(@RequestParam("power") int param,@RequestParam(name ="find_title", required = false) String find_title, Model model) {

        List<Noticeinfo> noticeinfos;

        if (find_title != null){
            // 根据标题模糊查询
            noticeinfos= iNoticeService.titleQueryNoticeInfo(find_title);
        }else {
            // 查询全部公告信息
            noticeinfos= iNoticeService.queryNoticeInfo();
        }
        model.addAttribute("noticeInfo",noticeinfos);
        // 管理员界面
        if (param == 1){
            return "notice/show_manage_NoticeInfo";
        }else
            // 学生界面
            return "notice/show_Stu_NoticeInfo";

    }

    /**
     * 删除公告
     * @param param
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/delete")
    public String deleteNoticeInfo(@RequestParam("power") int param,@RequestParam("id") int id, Model model) {

        boolean pd = iNoticeService.deleteNoticeInfo(id);
        // 删除失败
        if (!pd){
            model.addAttribute(Constants.MESSAGE,"删除失败");
        }else{
            model.addAttribute(Constants.MESSAGE,"删除成功");
        }

        model.addAttribute("noticeInfo",iNoticeService.queryNoticeInfo());
        // 管理员界面
        if (param == 1){
            return "notice/show_manage_NoticeInfo";
        }else
            // 学生界面
            return "notice/show_Stu_NoticeInfo";

    }

    /**
     * 前往公告添加页面
     * @return
     */
    @GetMapping("/toAddInfo")
    public String toAddInfo() {
        return "notice/addNoticeInfo";
    }

    /**
     * 新增公告
     * @param title
     * @param contact
     * @param detail
     * @param req
     * @return
     */
    @PostMapping("/addNoticeInfo")
    public String addNoticeInfo(@RequestParam("notice_title") String title,
                                @RequestParam("notice_contact") String contact,
                                @RequestParam("notice_detail") String detail,
                                HttpServletRequest req) {

        User user = (User) req.getSession().getAttribute(Constants.USER_SESSION);
        Noticeinfo noticeinfo = new Noticeinfo();
        noticeinfo.setNoticeTitle(title);
        noticeinfo.setNoticeContent(contact);
        noticeinfo.setNoticeDetail(detail);
        noticeinfo.setNoticePublisher(user.getUserDetail());
        noticeinfo.setNoticeDate(String.valueOf(LocalDate.now()));

        boolean pd =iNoticeService.addNoticeInfo(noticeinfo);

        if (!pd){
            req.getSession().setAttribute(Constants.MESSAGE,"添加失败");
        }else{
            req.getSession().setAttribute(Constants.MESSAGE,"添加成功");
        }
        return "redirect:/notice/listInfo?power="+user.getUserPower();
    }
}
